<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
    <title>Property</title>
    <link rel="stylesheet" href="../lib/Cesium-1.51/Build/Cesium/Widgets/widgets.css">
    <script src="../lib/Cesium-1.51/Build/Cesium/Cesium.js"></script>
    <style>
        html,
        body,
        #cesiumContainer {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
            background-color: #000000;
        }
    </style>
    <script src="bus.js"></script>
    <script src="../App/moment.js"></script>
    <script src="../utils/coorTransform.js"></script>

</head>

<body>
<div id="cesiumContainer"></div>
<script>
    var viewer = new Cesium.Viewer('cesiumContainer', {
        animation: true,
        timeline: true,
        shouldAnimate: true,
        imageryProvider: new Cesium.UrlTemplateImageryProvider({
            url: 'http://www.google.cn/maps/vt?lyrs=s@716&x={x}&y={y}&z={z}'
        })
    });

    viewer.camera.flyTo({
        destination: new Cesium.Cartesian3(-2284843.6893539904, 5030084.459916646, 3197419.22233534),
        orientation: {
            heading: 5.9824121245820825,
            pitch: -0.35321351463230544,
            roll: 6.282263350033762
        }
    });

    var startTime = Cesium.JulianDate.fromDate(moment("2099-01-01 01:00:00", "YYYY-MM-DD HH:mm:ss").toDate());
    var stopTime = Cesium.JulianDate.fromDate(moment("1999-01-01 01:01:01", "YYYY-MM-DD HH:mm:ss").toDate());
    var properties = [];

    busLines.forEach(function (busLine) {
        var tempStartTime = Cesium.JulianDate.fromDate(moment(busLine[0].gps_time, "YYYY-MM-DD HH:mm:ss").toDate());
        var tempStopTime = Cesium.JulianDate.fromDate(moment(busLine[busLine.length - 1].gps_time, "YYYY-MM-DD HH:mm:ss").toDate());

        if (tempStartTime < startTime) startTime = tempStartTime;
        if (tempStopTime > stopTime) stopTime = tempStopTime;

        var property = new Cesium.SampledPositionProperty();
        busLine.forEach(function (linePoint) {
            var time = Cesium.JulianDate.fromDate(moment(linePoint.gps_time, "YYYY-MM-DD HH:mm:ss").toDate());
            var gcj = coordinateTransform.bd09togcj02(parseFloat(linePoint.lng), parseFloat(linePoint.lat));
            var wg4 = coordinateTransform.gcj02towgs84(gcj[0], gcj[1]);
            var position = Cesium.Cartesian3.fromDegrees(wg4[0], wg4[1]);
            property.addSample(time, position);
        });
        properties.push(property);
    });

    console.log(startTime, stopTime);

    viewer.clock.startTime = startTime.clone();
    viewer.clock.stopTime = stopTime.clone();
    viewer.clock.currentTime = startTime.clone();
    viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP;
    viewer.clock.multiplier = 25;
    viewer.timeline.zoomTo(startTime, stopTime);

    properties.forEach(function (property, index) {
        viewer.entities.add({
            availability: new Cesium.TimeIntervalCollection([new Cesium.TimeInterval({
                start: startTime, stop: stopTime
            })]),
            position: property,
            orientation: new Cesium.VelocityOrientationProperty(property),
            model: {
                uri: '../models/Cesium_Ground.glb',
                minimumPixelSize: 24
            },
            label: {
                text: (index + 1) + "号班车",
                font: '10pt sans-serif',
                style: Cesium.LabelStyle.FILL_AND_OUTLINE, //FILL  FILL_AND_OUTLINE OUTLINE
                fillColor: Cesium.Color.YELLOW,
                showBackground: true,//指定标签后面背景的可见性
                backgroundColor: new Cesium.Color(0.165, 0.165, 0.165, 0.8), // 背景颜色
                backgroundPadding: new Cesium.Cartesian2(6, 6),//指定以像素为单位的水平和垂直背景填充padding
                pixelOffset: new Cesium.Cartesian2(0, -25)
            },
            path: {
                // leadTime: 0.2,
                // trailTime: 0.2,
                resolution: 1,
                width: 4,
                // material: new Cesium.PolylineGlowMaterialProperty({
                //     color: Cesium.Color.fromRandom()
                // })
                material: Cesium.Color.fromRandom({alpha: 0.8})
            }
        })
    });


    /**
     * ConstantProperty 常量值
     * @type {number}
     */
    /*var value = 0;

    var constantProperty = new Cesium.ConstantProperty();
    constantProperty.definitionChanged.addEventListener(function (args) {
        console.log(args.getValue());
    });

    window.setInterval(function () {
        constantProperty.setValue(value++); // 通过setValue改变值，并发射值改变的事件
    }, 1000);*/

    /**
     * CallbackProperty 回调
     * @type {Cesium.CallbackProperty}
     */
    // var callbackProperty = new Cesium.CallbackProperty(function (time, result) {
    //     console.log(time, result);
    // }, false);
    // window.setInterval(function () {
    //     callbackProperty.getValue(1,1);
    // }, 1000);


</script>
</body>

</html>